﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Configuration;
using CLibArtreve.bll;
using CLibArtreve.models;
using CLibArtreve.security;

namespace WebAppArtreve
{
    public partial class ArtView : System.Web.UI.Page
    {
        //VARIABLES
        private static Int64 _artId;
        private static string _connectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;
            }
        }
        private static Int64? _userId
        {
            get
            {
                ArtreveIdentity identity = (ArtreveIdentity)HttpContext.Current.User.Identity;
                return identity.User.Id;
            }
        }
        //private int ratingUserStars = -1;

        //PAGE LOAD
        protected void Page_Load(object sender, EventArgs e)
        {
            _artId = Convert.ToInt64(Request.QueryString["Id"]);
        }

        //GET GLOBAL RATING
        [WebMethod]
        public static string GetStarsAllUsers()
        {
            try
            {
                //1. BLL
                //-----------------------
                string connectionString = ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;

                RatingBLL bllRating = new RatingBLL(connectionString);

                //GET RATING BY ART ID
                IList<Rating> listRating;
                listRating = bllRating.GetRatingByArtId(_artId);

                //2. RETURN RATING
                //-----------------------
                if (listRating.Count > 0)
                {
                    //VARIABLES
                    double stars = 0;
                    int i;

                    //COUNT RATING: ALL STARS
                    for (i = 0; i < listRating.Count; i++)
                    {
                        stars += listRating[i].Stars;
                    }

                    //RETURN STARS TO SHOW THEM (AFTER DIVIDING STARS BY NUMBER OF USERS THAT VOTED)
                    return Convert.ToString(Math.Round(stars / i, 2));
                }
                else
                {
                    return "0";
                }
                
            }
            catch (Exception ex)
            {
                return "0";
                throw (ex);
            }

        }

        //GET USER RATING
        [WebMethod]
        public static string GetStarsUser()
        {
            try
            {
                RatingBLL bllRating = new RatingBLL(_connectionString);
                Rating rating = new Rating();
                rating = bllRating.GetRatingByArtIdAndUserId(_userId, _artId);

                //RETURN STARS TO SHOW THEM
                if (rating != null)
                {
                    int ratingUserStars = rating.Stars - 1;
                    return Convert.ToString(ratingUserStars);
                }
                else
                {
                    return "-1";
                }
            }
            catch (Exception ex)
            {
                return "-1";
                throw (ex);
            }
        }

        //SET USER RATING        
        [WebMethod]
        public static string SetStars(int stars)
        {
            try
            {
                //1. BLL
                //-----------------------
                RatingBLL bllRating = new RatingBLL(_connectionString);

                //2. INSERT RATING IN MODEL
                //-----------------------
                Rating rating = new Rating();
                rating.ArtId = _artId;
                rating.Stars = stars;
                rating.UserId = _userId;

                //3. INSERT OR UPDATE MODEL IN DB
                //-----------------------
                int ratingUserStars = Convert.ToInt32(GetStarsUser());

                if (ratingUserStars == -1)
                {
                    bllRating.InsertRating(rating);
                }
                else
                {
                    bllRating.UpdateArtByArtIdAndUserId(rating);
                }

                //RETURN STARS TO SHOW THEM
                return Convert.ToString(stars - 1);
            }
            catch (Exception ex)
            {
                return "0";
                throw (ex);
            }
        }
    }
}